package com.carpedil.smvc.dao;

import com.carpedil.smvc.po.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/** @author Administrator */
@Repository
public interface UserMapper extends JpaRepository<User, Integer> {
  /**
   * findUserById
   *
   * @param id id
   * @return User
   */
  User findUserById(Integer id);

  User findUserByUserNameContaining(String userName);

  @Query(value = "from User where userName=?1 and passWord=?2")
  User getUser1(String userName, String passWord);

  @Query(value = "from User where userName=:userName and passWord=:passWord")
  User getUser2(@Param("userName") String userName, @Param("passWord") String passWord);

  @Query(value = "select * from users where user_name=?1 and pass_word=?2", nativeQuery = true)
  User getUserByNativeSql(String userName, String passWord);

  @Modifying
  @Query(value = "update User set userName=?1,passWord=?2 where id=?3")
  public void updateUser1(String userName, String passWord, Integer id);

  @Modifying
  @Query(value = "update User set userName=:userName,passWord=:passWord where id=:id")
  public void updateUser2(
      @Param("userName") String userName,
      @Param("passWord") String passWord,
      @Param("id") Integer id);

  @Modifying
  @Query(value = "update users set user_name=?1,pass_word=?2 where id=?3", nativeQuery = true)
  @Transactional(rollbackFor = Exception.class)
  public void updateUserByNativeSql(String userName, String passWord, Integer id);
}
